﻿using Bear.Library.WinApi;
using Bear.Models;
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bear.MapleStory
{
    public class LoliCode
    {
        public static Image<Gray, byte> Capture()
        {
            DataCenter dc = DataCenter.Load();
            return Capture(dc.Handle);
        }


        public static Image<Gray, byte> Capture(IntPtr handle)
        {
            DataCenter dc = DataCenter.Load();


            if (WinApiHelper.IsWindow(handle) == 0)
            {
                throw new WindowException();
            }

            lock (dc._ScreenshotLock)
            {
                Bitmap bmp = Bear.Library.Util.ScreenCapturer.Capture(handle);
                Image<Gray, byte> result = new Image<Gray, byte>(bmp);
                bmp.Dispose();
                return result;
            }
        }


        public static Bitmap Capture(Map map)
        {
            Point point = new Point(map.Left, map.Top);
            Size size = new Size(map.Width, map.Height);
            return Capture(point, size);
        }


        public static Bitmap Capture(LifeAndMagic lm)
        {
            Point point = new Point(lm.Left, lm.Top);
            Size size = new Size(lm.Width, lm.Height);
            return Capture(point, size);
        }

        public static Bitmap Capture(Point point, Size size)
        {
            DataCenter dc = DataCenter.Load();
            if (WinApiHelper.IsWindow(dc.Handle) == 0)
            {
                throw new WindowException();
            }
            lock (dc._ScreenshotLock)
            {
                Bitmap bmp = Bear.Library.Util.ScreenCapturer.Capture(dc.Handle, point, size);
                return bmp;
            }
        }




        public static string Bcdedit()
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "bcdedit.exe";
            p.StartInfo.Arguments = "/set testsigning on";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            p.Start();
            p.WaitForExit();
            return p.StandardOutput.ReadToEnd();
        }

        public static void ShowFileInfo()
        {
            SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
            info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
            info.lpVerb = "properties";
            info.lpFile = "WinIo64.sys";
            info.nShow = ApiEnums.SW_SHOW;
            info.fMask = ApiEnums.SEE_MASK_INVOKEIDLIST;
            WinApiHelper.ShellExecuteEx(ref info);
        }

        public static void Serialization<T>(T obj, string filePath)
        {
            using (FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                BinaryFormatter format = new BinaryFormatter();
                format.Serialize(stream, obj);
            }
        }

        public static T Deserialization<T>(string fileName)
        {

            using (FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                BinaryFormatter format = new BinaryFormatter();
                return (T)format.Deserialize(stream);
            }

        }

    }
}
